// pages/my/orderList.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "我的订单", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl:''
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    tabList: [{
      id: 0,
      name: '全部'
    }, {
      id: 1,
      name: '待使用'
    }, {
      id: 2,
      name: '已完成'
    }, {
      id: 3,
      name: '退款中'
    }, {
      id: 4,
      name: '已过期'
    }],
    tabId: 0,
    orderList: [],
    orderDsyList: [],
    orderYsyList: [],
    orderTotalList: [],

    openid: '',
    shareTitle: '',
    shareImg: ''
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  tab(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let orderList = [];
    if (id == 0) {
      //全部
      orderList = that.data.orderTotalList
    } else if (id == 1) {
      //待使用
      orderList = that.data.orderDsyList
    } else {
      //已使用 or 已过期
      orderList = that.data.orderYsyList
    }
    that.setData({
      tabId: id,
      orderList: orderList
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    var headerFlag = options.headerFlag;
    var id = options.id;
    if(headerFlag=='cj'){
      that.setData({
        ['defaultData.goUrl']: '/pages/index/index'
      });
    }else if(headerFlag=='ms'){
      that.setData({
        ['defaultData.goUrl']: '/pages/index/index'
      });
    }else if(headerFlag=='kd'){
      that.setData({
        ['defaultData.goUrl']: '/pages/index/index'
      });
    }else if(headerFlag=='space'){
      that.setData({
        ['defaultData.goUrl']: '/pages/index/index'
      });
    }else{
      that.setData({
        ['defaultData.goUrl']: '/pages/my/index'
      });
    }
    

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getOrderListPageInfo(res.data.openid);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getOrderListPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },


  //获取本业数据
  getOrderListPageInfo(openid) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getOrderListPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据

          that.setData({
            orderTotalList: res.data.orderTotalList,
            orderDsyList: res.data.orderDsyList,
            orderYsyList: res.data.orderYsyList,
            orderList: res.data.orderTotalList
          })
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  //跳转至订单详情页
  goDetail(e) {
    var that = this;
    var flag = e.currentTarget.dataset.flag;
    var id = e.currentTarget.dataset.id;
    //判断跳转的页面
    if(flag=='cj'||flag=='ms'){
      //抽奖或秒杀
      wx.navigateTo({
        url: '/pages/lotteryDrawPage/LotteryOrderDetail/index?id=' + id + "&type=" + flag
      });
    }else  if(flag=='kd'){
      //快递预约
       wx.navigateTo({
        url: '/pages/expressReservation/detail/index?id=' + id
      });
    }else if(flag=='space'){
      //空间预约
       wx.navigateTo({
        url: '/pages/orderCenter/siteOrder/spaceDetail/detail?id=' + id
      });
    }else{
      //其他预约
      wx.navigateTo({
        url: '/pages/my/orderDetail?id=' + id + "&type=" + flag
      });
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})